############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2018
## Figure A5: Same as Figure 1, but Democratic Candidate Emergence Only
## Figure A6: Same as Figure 1, but Republican Candidate Emergence Only
############################################################

# Required Libraries
require(ggplot2)

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

## FigA5a, Upper Left Panel of Figure A5: Whites
sl_dataY <- subset(sl_data, white_incprop >= 0.25)
sl_dataN <- subset(sl_data, white_incprop < 0.25)

figA5a <- ggplot(data=sl_data, aes(x=white_pct, y=white_cand_D))
figA5a <- figA5a + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA5a <- figA5a + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA5a <- figA5a + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA5a <- figA5a + ggtitle("Whites")
figA5a <- figA5a + scale_x_continuous("Percent White in State Legislative District")
figA5a <- figA5a + scale_y_continuous("Probability of White Candidate")
figA5a <- figA5a + coord_cartesian(ylim=c(-0.01, 1.01))
figA5a <- figA5a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA5a

## FigA5b, Upper Right Panel of Figure A5: African-Americans
sl_dataY <- subset(sl_data, black_incprop >= 0.25)
sl_dataN <- subset(sl_data, black_incprop < 0.25)

figA5b <- ggplot(data=sl_data, aes(x=black_pct, y=black_cand_D))
figA5b <- figA5b + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA5b <- figA5b + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA5b <- figA5b + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA5b <- figA5b + ggtitle("African-Americans")
figA5b <- figA5b + scale_x_continuous("Percent Black in State Legislative District")
figA5b <- figA5b + scale_y_continuous("Probability of Black Candidate")
figA5b <- figA5b + coord_cartesian(ylim=c(-0.01, 1.01))
figA5b <- figA5b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA5b

## FigA5c, Lower Left Panel of Figure A5: Latinos
sl_dataY <- subset(sl_data, latino_incprop >= 0.25)
sl_dataN <- subset(sl_data, latino_incprop < 0.25)

figA5c <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand_D))
figA5c <- figA5c + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA5c <- figA5c + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA5c <- figA5c + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA5c <- figA5c + ggtitle("Latinos")
figA5c <- figA5c + scale_x_continuous("Percent Latino in State Legislative District")
figA5c <- figA5c + scale_y_continuous("Probability of Latino Candidate")
figA5c <- figA5c + coord_cartesian(ylim=c(-0.01, 1.01))
figA5c <- figA5c + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA5c

## FigA5d, Lower Right Panel of Figure A5: Asians
sl_dataY <- subset(sl_data, asian_incprop >= 0.25)
sl_dataN <- subset(sl_data, asian_incprop < 0.25)

figA5d <- ggplot(data=sl_data, aes(x=asian_pct, y=asian_cand_D))
figA5d <- figA5d + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA5d <- figA5d + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA5d <- figA5d + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA5d <- figA5d + ggtitle("Asians")
figA5d <- figA5d + scale_x_continuous("Percent Asian in State Legislative District")
figA5d <- figA5d + scale_y_continuous("Probability of Asian Candidate")
figA5d <- figA5d + coord_cartesian(ylim=c(-0.01, 1.01))
figA5d <- figA5d + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA5d

## FigA6a, Upper Left Panel of Figure A6: Whites
sl_dataY <- subset(sl_data, white_incprop >= 0.25)
sl_dataN <- subset(sl_data, white_incprop < 0.25)

figA6a <- ggplot(data=sl_data, aes(x=white_pct, y=white_cand_R))
figA6a <- figA6a + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA6a <- figA6a + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA6a <- figA6a + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA6a <- figA6a + ggtitle("Whites")
figA6a <- figA6a + scale_x_continuous("Percent White in State Legislative District")
figA6a <- figA6a + scale_y_continuous("Probability of White Candidate")
figA6a <- figA6a + coord_cartesian(ylim=c(-0.01, 1.01))
figA6a <- figA6a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA6a

## FigA6b, Upper Right Panel of Figure A6: African-Americans
sl_dataY <- subset(sl_data, black_incprop >= 0.25)
sl_dataN <- subset(sl_data, black_incprop < 0.25)

figA6b <- ggplot(data=sl_data, aes(x=black_pct, y=black_cand_R))
figA6b <- figA6b + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA6b <- figA6b + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA6b <- figA6b + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA6b <- figA6b + ggtitle("African-Americans")
figA6b <- figA6b + scale_x_continuous("Percent Black in State Legislative District")
figA6b <- figA6b + scale_y_continuous("Probability of Black Candidate")
figA6b <- figA6b + coord_cartesian(ylim=c(-0.01, 1.01))
figA6b <- figA6b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA6b

## FigA6c, Lower Left Panel of Figure A6: Latinos
sl_dataY <- subset(sl_data, latino_incprop >= 0.25)
sl_dataN <- subset(sl_data, latino_incprop < 0.25)

figA6c <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand_R))
figA6c <- figA6c + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA6c <- figA6c + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA6c <- figA6c + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA6c <- figA6c + ggtitle("Latinos")
figA6c <- figA6c + scale_x_continuous("Percent Latino in State Legislative District")
figA6c <- figA6c + scale_y_continuous("Probability of Latino Candidate")
figA6c <- figA6c + coord_cartesian(ylim=c(-0.01, 1.01))
figA6c <- figA6c + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA6c

## FigA6d, Lower Right Panel of Figure A6: Asians
sl_dataY <- subset(sl_data, asian_incprop >= 0.25)
sl_dataN <- subset(sl_data, asian_incprop < 0.25)

figA6d <- ggplot(data=sl_data, aes(x=asian_pct, y=asian_cand_R))
figA6d <- figA6d + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA6d <- figA6d + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA6d <- figA6d + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA6d <- figA6d + ggtitle("Asians")
figA6d <- figA6d + scale_x_continuous("Percent Asian in State Legislative District")
figA6d <- figA6d + scale_y_continuous("Probability of Asian Candidate")
figA6d <- figA6d + coord_cartesian(ylim=c(-0.01, 1.01))
figA6d <- figA6d + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA6d